﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CellarDoor.Gui.Elements
{
    public class UiRadioButton : UiCheckBox
    {
        /// <summary>
        /// A simple Radio Button
        /// </summary>
        /// <param name="group"></param>
        public UiRadioButton()
            : base()
        {
            Initialize();
        }

        /// <summary>
        /// A simple Radio Button
        /// </summary>
        /// <param name="group"></param>
        public UiRadioButton(UiElement element)
            : base(element)
        {
            Initialize();
        }

        private void Initialize()
        {
            ElementName = "Radiobutton";
        }

        protected override void OnMouseClick(UiMouseProxy mouse)
        {
            // treat parent control as radio group
            // and uncheck all radiobuttons
            if (Parent != null)
            {
                ClearChecked(Parent);
            }
            // the base logic will check this element
            base.OnMouseClick(mouse);
        }

        public static UiRadioButton GetChecked(UiElement group)
        {
            foreach (var item in group.Elements)
            {
                var rb = item as UiRadioButton;
                if (rb != null && rb.Checked)
                {
                    return rb;
                }
            }
            return null;
        }

        public static void ClearChecked(UiElement group)
        {
            foreach (var item in group.Elements)
            {
                var rb = item as UiRadioButton;
                if (rb != null && rb.Checked)
                {
                    rb.Checked = false;
                }
            }
        }
    }
}
